<template>
	<view class="scroll-view-container">
		<scroll-view class="scroll-view" scroll-x :scroll-into-view="scrollIntoView" scroll-with-animation>
			<view :id="'view_' + index" class="scroll-view-item" :class="{'active':active === index}"
				v-for="(item,index) in list" :key="index" @tap.stop="handleClick(index)">
				<text v-if="item.label">{{item.label}}</text>
				<text v-else>{{item}}</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		props: {
			list: {
				type: Array,
				default: () => []
			},
		},
		data() {
			return {
				scrollIntoView: `view_0`,
				active: 0
			}
		},
		methods: {
			setCurrent(index){
				this.scrollIntoView = `view_${index}`
				this.active = index
			},
			handleClick(index) {
				if (this.active === index) return
				this.scrollIntoView = `view_${index}`
				// this.$emit('update:value', index)
				this.active = index
				this.$emit('change', index)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.scroll-view-container {
		background-color: #fff;
		height: 100rpx;
		line-height: 100rpx;
		border-bottom: 2rpx solid $u-border-color;
		padding-left: 32rpx;
	}

	.scroll-view {
		white-space: nowrap;
		width: 100%;

		&-item {
			display: inline-block;
			padding: 0 32rpx;
			height: 64rpx;
			text-align: center;
			line-height: 64rpx;
			background: #fff;
			border-radius: 32rpx;
			background-color: $u-primary-light;

			&:last-child {
				margin-right: 32rpx;
			}
		}

		.active {
			color: #fff;
			background-color: $u-primary;
		}
	}

	.scroll-view-item+.scroll-view-item {
		margin-left: 16rpx;
	}
</style>